package com.lizicloud.infrastructure.common.utils;

import java.util.Random;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;

/**
 * 随机数工具类
 */
public class RandomUtils {
    
    private static final Random RANDOM = new Random();
    
    /**
     * 重置随机数生成器
     */
    public static void reset() {
        // 重置随机数生成器
        RANDOM.setSeed(System.currentTimeMillis());
    }
    private static final char[] CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".toCharArray();
    private static final char[] NUMBERS = "0123456789".toCharArray();
    private static final char[] LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray();
    
    /**
     * 生成随机整数
     */
    public static int nextInt() {
        return RANDOM.nextInt();
    }
    
    /**
     * 生成指定范围的随机整数
     */
    public static int nextInt(int bound) {
        return RANDOM.nextInt(bound);
    }
    
    /**
     * 生成指定范围的随机整数
     */
    public static int nextInt(int min, int max) {
        if (min >= max) {
            throw new IllegalArgumentException("最小值必须小于最大值");
        }
        return min + RANDOM.nextInt(max - min);
    }
    
    /**
     * 生成随机长整数
     */
    public static long nextLong() {
        return RANDOM.nextLong();
    }
    
    /**
     * 生成指定范围的随机长整数
     */
    public static long nextLong(long min, long max) {
        if (min >= max) {
            throw new IllegalArgumentException("最小值必须小于最大值");
        }
        return min + (long) (RANDOM.nextDouble() * (max - min));
    }
    
    /**
     * 生成随机双精度数
     */
    public static double nextDouble() {
        return RANDOM.nextDouble();
    }
    
    /**
     * 生成指定范围的随机双精度数
     */
    public static double nextDouble(double min, double max) {
        if (min >= max) {
            throw new IllegalArgumentException("最小值必须小于最大值");
        }
        return min + RANDOM.nextDouble() * (max - min);
    }
    
    /**
     * 生成随机布尔值
     */
    public static boolean nextBoolean() {
        return RANDOM.nextBoolean();
    }
    
    /**
     * 生成随机字符串
     */
    public static String nextString(int length) {
        if (length <= 0) {
            throw new IllegalArgumentException("长度必须大于0");
        }
        
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            sb.append(CHARS[RANDOM.nextInt(CHARS.length)]);
        }
        return sb.toString();
    }
    
    /**
     * 生成随机数字字符串
     */
    public static String nextNumberString(int length) {
        if (length <= 0) {
            throw new IllegalArgumentException("长度必须大于0");
        }
        
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            sb.append(NUMBERS[RANDOM.nextInt(NUMBERS.length)]);
        }
        return sb.toString();
    }
    
    /**
     * 生成随机字母字符串
     */
    public static String nextLetterString(int length) {
        if (length <= 0) {
            throw new IllegalArgumentException("长度必须大于0");
        }
        
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            sb.append(LETTERS[RANDOM.nextInt(LETTERS.length)]);
        }
        return sb.toString();
    }
    
    /**
     * 生成随机验证码（数字）
     */
    public static String nextVerificationCode(int length) {
        return nextNumberString(length);
    }
    
    /**
     * 从数组中随机选择一个元素
     */
    public static <T> T nextElement(T[] array) {
        if (array == null || array.length == 0) {
            return null;
        }
        return array[RANDOM.nextInt(array.length)];
    }
    
    /**
     * 从列表中随机选择一个元素
     */
    public static <T> T nextElement(List<T> list) {
        if (list == null || list.isEmpty()) {
            return null;
        }
        return list.get(RANDOM.nextInt(list.size()));
    }
    
    /**
     * 打乱数组顺序
     */
    public static <T> void shuffle(T[] array) {
        if (array == null || array.length <= 1) {
            return;
        }
        
        for (int i = array.length - 1; i > 0; i--) {
            int j = RANDOM.nextInt(i + 1);
            T temp = array[i];
            array[i] = array[j];
            array[j] = temp;
        }
    }
    
    /**
     * 打乱列表顺序
     */
    public static <T> void shuffle(List<T> list) {
        if (list == null || list.size() <= 1) {
            return;
        }
        Collections.shuffle(list, RANDOM);
    }
    
    /**
     * 生成随机颜色（RGB）
     */
    public static String nextColor() {
        int r = nextInt(256);
        int g = nextInt(256);
        int b = nextInt(256);
        return String.format("#%02X%02X%02X", r, g, b);
    }
    
    /**
     * 生成随机IP地址
     */
    public static String nextIpAddress() {
        return String.format("%d.%d.%d.%d", 
                           nextInt(1, 255), 
                           nextInt(0, 255), 
                           nextInt(0, 255), 
                           nextInt(1, 255));
    }
    
    /**
     * 生成随机端口号
     */
    public static int nextPort() {
        return nextInt(1024, 65536);
    }
    
    private RandomUtils() {
        // 私有构造函数，防止实例化
    }
}